package de.breitbeil.jbooking.ui.form;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.binding.form.FormModel;
import org.springframework.richclient.settings.Settings;

import de.breitbeil.jbooking.ui.preferences.SettingsFormObject;

public class ExcelExportFormObject implements SettingsFormObject {

    private String settingsId;
    private String entitiy;
    
    private List<String> properties;
    private String filename;
    
    public ExcelExportFormObject(String entity) {
        this.settingsId = "excelExport" + entity;
        this.entitiy = entity;
    }

    public void setProperties(List<String> properties) {
        this.properties = properties;
    }
    public List<String> getProperties() {
        return properties;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }
    public String getFilename() {
        return filename;
    }

    public void fillDefaults(FormModel formModel) {
        StringBuilder defaultFileName = new StringBuilder();
        defaultFileName.append(System.getProperty("user.home"));
        defaultFileName.append(System.getProperty("file.separator"));
        defaultFileName.append("JBooking" +entitiy+ ".xls");
        
        formModel.getValueModel("properties").setValue(new HashSet<String>());
        formModel.getValueModel("filename").setValue(defaultFileName.toString());
    }

    public void init(Settings settings) {
        String exportSettings = settings.getString(settingsId + "properties");
        List<String> selectedProperties = Arrays.asList(exportSettings.split(";"));
        
        properties = selectedProperties;
        filename = settings.getString(settingsId + "filename");
    }

    public void save(Settings settings) {
        StringBuilder builder = new StringBuilder();
        for(String s: properties) {
            builder.append(s);
            builder.append(";");
        }
        
        settings.setString(settingsId + "properties", builder.toString());
        settings.setString(settingsId + "filename", filename);
    }

}
